package com.proton.protonecgsdkdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.proton.bluetooth.utils.Logger;
import com.proton.ecgcard.algorithm.bean.RealECGData;
import com.proton.ecgcard.connector.EcgCardManager;
import com.proton.ecgcard.connector.callback.DataListener;
import com.proton.view.EcgRealTimeView;
import com.wms.ble.bean.ScanResult;
import com.wms.ble.callback.OnConnectListener;
import com.wms.ble.callback.OnScanListener;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private EcgRealTimeView ecgView;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ecgView = findViewById(R.id.id_ecg_view);

        //todo 搜索设备需要定位权限，android6.0及其以上需要动态申请权限，请自行申请一下，
        //todo 或者手机权限管理里面直接允许定位权限
        PermissionUtils.getLocationPermission(this);
        final EcgCardManager manager = EcgCardManager.getInstance("你要连接的mac地址");
        findViewById(R.id.id_search_device).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EcgCardManager.scanDevice(new OnScanListener() {
                    @Override
                    public void onScanStart() {
                        Log.e(TAG, "onScanStart...");
                    }

                    @Override
                    public void onDeviceFound(ScanResult scanResult) {
                        Log.w(TAG, "onDeviceFound: " + scanResult.getDevice().getAddress());
                    }

                    @Override
                    public void onScanStopped() {
                        Log.e(TAG, "onScanStopped...");
                    }

                    @Override
                    public void onScanCanceled() {
                        Log.e(TAG, "onScanCanceled...");
                    }
                });
            }
        });

        findViewById(R.id.id_start_measure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                manager.setDataListener(new DataListener() {
                    @Override
                    public void receiveTouchMode(int mode) {
                        super.receiveTouchMode(mode);
                        //mode = 0代表双手没有触摸 mode = 1代表双手触摸
                        Log.w(TAG, "receiveTouchMode: mode = " + mode);
                    }

                    @Override
                    public void receiveEcgFilterData(RealECGData currentData, int section) {
                        super.receiveEcgFilterData(currentData, section);
                        //currentData.ecgData 用来绘制心电图
                        Log.w(TAG, "接收心电数据用来绘制实时心电图" + currentData.ecgData.size());
                        ecgView.addEcgData(currentData.ecgData);
                        if (!ecgView.isRunning()) {
                            ecgView.startDrawWave();
                        }
                    }

                    @Override
                    public void receiverHeartRate(int rate) {
                        super.receiverHeartRate(rate);
                        Log.w(TAG, "当前心跳:" + rate);
                    }

                    @Override
                    public void receiveEcgSourceData(List<Float> sourceData) {
                        //这个是心电卡采集的原始数据，没有经过滤波算法
                        super.receiveEcgSourceData(sourceData);
                    }

                    @Override
                    public void signalInterference(int signalQualityIndex) {
                        super.signalInterference(signalQualityIndex);
                        Log.w(TAG, "信号强度:" + signalQualityIndex);
                    }

                    @Override
                    public void receiveBFR(Integer bfr) {
                        super.receiveBFR(bfr);
                        //心电卡采集到的体脂电阻
                    }

                    @Override
                    public void receiveBattery(Integer battery) {
                        super.receiveBattery(battery);
                        Log.w(TAG, "电量:" + battery);
                    }

                    @Override
                    public void receiveSerial(String serial) {
                        super.receiveSerial(serial);
                        Log.w(TAG, "序列号:" + serial);
                    }

                    @Override
                    public void receiveHardVersion(String hardVersion) {
                        super.receiveHardVersion(hardVersion);
                        Log.w(TAG, "固件版本:" + hardVersion);
                    }
                });
                //开始连接
                //todo 需要将第一个参数替换为你自己的心电卡设备mac地址
                manager.connectEcgCard(new OnConnectListener() {
                    @Override
                    public void onConnectSuccess() {
                        Logger.w("连接成功");
                    }

                    @Override
                    public void onConnectFaild() {
                        Logger.w("连接失败");
                    }

                    @Override
                    public void onDisconnect() {
                        Logger.w("连接断开");
                    }
                });
            }
        });
    }
}
